package @@packageName@@;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

public class JsonizerHelper {
    private JSONObject references = new JSONObject();

    private Map<String, Object> refToObj = new HashMap<String, Object>();
    private Map<Object, String> objects = new HashMap<Object, String>();

    int counter = 0;

    /**
     * See if this object instance has already been serialised and returns a reference number to it.
     * 
     * @param arg0
     * @return
     */
    public String fetchRef(Object arg0) {
        return objects.get(arg0);
    }

    /**
     * Create and store a new reference number for this object instance.
     * 
     * @param arg0
     * @return
     */
    public String createRef(Object arg0) {
        String ref = Integer.toHexString(counter++);
        objects.put(arg0, ref);
        refToObj.put(ref, arg0);

        return ref;
    }

    /**
     * Get the object by reference number.
     * 
     * @param ref
     * @return
     */
    public Object getObject(String ref) {
        return refToObj.get(ref);
    }

    /**
     * Store the object with a given reference number.
     * 
     * @param ref
     * @param arg0
     */
    public void putObject(String ref, Object arg0) {
        objects.put(arg0, ref);
        refToObj.put(ref, arg0);
    }

    public void putJson(String ref, JSONValue json) {
        references.put(ref, json);
    }

    public JSONValue getJson(String ref) {
        return references.get(ref);
    }

    public JSONObject getReferences() {
        return references;
    }

    public void setReferences(JSONObject references) {
        this.references = references;
    }
}
